package com.dtjd.playcoinmonkey.activities;

import android.Manifest;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.View;

import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityOptionsCompat;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.request.RequestOptions;
import com.dtjd.playcoinmonkey.MyApplication;
import com.dtjd.playcoinmonkey.R;
import com.dtjd.playcoinmonkey.databinding.ActivityAccountBinding;
import com.dtjd.playcoinmonkey.utils.Tools;

import java.util.Objects;

public class AccountActivity extends AppCompatActivity {
    private ActivityResultLauncher<Intent> launcher;
    private ActivityAccountBinding binding;
        @RequiresApi(api = Build.VERSION_CODES.R)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityAccountBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        binding.accountIcon.setOnClickListener(view -> startIcon());
        if (MyApplication.user!=null){
            binding.accountNicknameTv.setText(MyApplication.user.wb_nickname);
            binding.accountAccountTv.setText(MyApplication.user.wu_name);
            Glide.with(this).load(MyApplication.user.wb_url).apply(RequestOptions.bitmapTransform(new CircleCrop())).placeholder(R.drawable.loading).placeholder(R.drawable.loading).error(R.drawable.loaderr).into(binding.accountIcon);
        }
        binding.accountModifyIcon.setOnClickListener(view -> startIcon());
        binding.accountModifyNickname.setOnClickListener(view -> launcher.launch(new Intent(AccountActivity.this,ModifyNicknameActivity.class),ActivityOptionsCompat.makeSceneTransitionAnimation(this, binding.accountNicknameTv, "nickname")));
        binding.accountBack.setOnClickListener(view -> finish());
        launcher=registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), result -> {
            if (result.getResultCode()==3){
                String nickname = Objects.requireNonNull(result.getData()).getStringExtra("nickname");
                MyApplication.user.wb_nickname=nickname;
                binding.accountNicknameTv.setText(nickname);
            }
            if (result.getResultCode()==4) {
                binding.accountIcon.setImageURI(Objects.requireNonNull(result.getData()).getData());
            }
        });
    }

    @RequiresApi(api = Build.VERSION_CODES.R)
    private void startIcon() {
        if (Tools.hasExternalStoragePermission(this)){
            launcher.launch(new Intent(AccountActivity.this,LookIconActivity.class),ActivityOptionsCompat.makeSceneTransitionAnimation(this, binding.accountIcon, "icon"));
        }else {
            requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE,Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS,Manifest.permission.MANAGE_EXTERNAL_STORAGE}, 0);
        }
    }
}